<!-- 需要关联的客户选择组件 -->
<template>
	<div>
		<u-popup safe-area-inset-bottom v-model="show" mode="left">
			<view style="">
				<uni-nav-bar shadow left-icon="left" left-text="返回" right-text="确定" :title="title"
					@clickLeft="show=false" @clickRight="customerChoiceConfirm" fixed />
			</view>

			<!-- 搜索区域 -->
			<div style="position: fixed;width: 99%;background-color: #fff;z-index: 999;">
				<!-- 滑动二级导航栏 -->
				<view class="log-tabbar-content">
					<view class="log-tabbar-list">
						<scroll-view scroll-x="true" class="log-shop-item" :show-scrollbar="true">
							<u-tag v-if="CustomerList.length==0" style="margin-right: 20rpx;margin-bottom: 10rpx;"
								:text="'未选择客户'" type="error" />
							<block v-for="(item, index) in CustomerList" :key="index">
								<u-tag style="margin-right: 20rpx;margin-bottom: 10rpx;" :text="item.cCusName"
									type="primary" closeable @close="tagCloseChange($event, item, index)" />
							</block>
						</scroll-view>
					</view>
				</view>
			</div>
			<div class="outerBox" style="width:300px;">
				<scroll-view style="width: 100%;height: 100%;padding-top: 52rpx;" scroll-y="true"
					@scrolltolower="lower">
					<div class="ckbxcli" @click="customerChoice(item)" v-for="(item, index) in data" :key="index">
						<div style="text-align: left;">
							<view style="display: flex;justify-content: space-between;">
								<view span="20">
									<span style="font-size: 14px;font-weight: bold">{{ item.cCusName }}</span>
								</view>
								<view>
									<!-- 暂时去掉  后续根据需求操作 -->
									<!-- 	<p>
										<uni-tag v-if="item.cCusType === -1" type="primary" :text="'备案中'" inverted></uni-tag>
										<uni-tag v-else-if="item.cCusType === 0" type="primary" :text="'正式'" inverted></uni-tag>
										<uni-tag v-else-if="item.cCusType === 1" type="primary" :text="'潜在'" inverted></uni-tag>
										<uni-tag v-else-if="item.cCusType === 2" type="primary" :text="'休眠'" inverted></uni-tag>
										<uni-tag v-else-if="item.cCusType === 3" type="primary" :text="'待转正'" inverted></uni-tag>
									</p> -->
								</view>
							</view>
							<u-row style="">
								<u-col>
									<p style="">客户编码：<span>{{ item.cCusCode }}</span></p>
								</u-col>
							</u-row>
							<u-row style="">
								<u-col span="11">
									<p style="">备案人：<span>{{ item.RealName }}</span></p>
								</u-col>
								<u-col span="13">
									<view style="">备案时间： <uni-dateformat :date="item.CreateTime"></uni-dateformat>
									</view>
								</u-col>
							</u-row>

						</div>
					</div>
				</scroll-view>
			</div>
		</u-popup>
	</div>
</template>

<script>
	// import PaginationCom from "@/components/PaginationCom.vue";
	import {
		GetDataListForCustomerInfoRetrieval
	} from '@/api/interface.js'
	export default {
		props: {
			// 弹窗标题
			title: {
				type: String,
				default: '选择客户'
			},
			// 是否隐藏客户选择搜索栏
			isHidden: {
				type: Boolean,
				default: false,
			},
			isAllData: {
				default: false,
				type: Boolean
			} // 是否查询全部数据
		},
		components: {
			// PaginationCom,
			// "van-pagination": Pagination,
		},
		data() {
			return {
				loading: false,
				queryParam: {
					cCusName: "",
					cCusType: null,
					isAllData: this.isAllData
				},
				pagination: {
					current: 1,
					pageSize: 10,
					Total: 0,
				},
				entity: {},
				judgment: true,
				data: [],
				show: false,
				Id: "",
				PageRows: 20,
				isSearchUser: false,
				// 搜索出来的客户列表
				CustomerList: []
			};
		},
		onLoad() {
			// 组件创建的时候初始默认检索提示
			this.isSearchUser = true
		},
		methods: {
			// 选择客户标签阿牛关闭出发
			tagCloseChange(event, item, index) {
				console.log('关闭了', event, item, index)
				this.CustomerList.splice(index, 1)
			},
			// 触底
			lower(e) {
				if (this.data.length >= this.pagination.Total) return
				// 上拉加载
				this.pagination.current++;
				this.getDataList()
			},
			onLoading() {
				this.loading = true; //防止第一页重复加载
				// 获取客户列表
				this.getDataList();
			},
			paginationchange(a) {
				// a:当前选中页码
				this.pagination.current = a;
				this.getDataList();
			},
			//返回
			onClickLeft() {
				this.show = false;
				// 返回时候显示检索提示
				this.isSearchUser = true
				//列表 数据重置
				this.queryParam.cCusName = ""
				this.data = []
			},
			close() {
				this.show = false;
			},
			// 取消
			cancel() {
				this.queryParam.cCusName = ""
				this.data = [];
				this.status = "noMore"
				this.isSearchUser = true
			},
			onSearch() {
				if (this.queryParam.cCusName) {
					this.search(this.queryParam.cCusName)
				} else {
					this.search
				}

			},
			//搜索
			search(value) {
				this.isSearchUser = false
				this.pagination.current = 1
				this.data = []
				this.status = "loading"
				this.getDataList();
			},

			// 选择客户确定后
			customerChoice(item) {
				console.log('先选择客户好了------', item, this.CustomerList, this.data);
				// 第一种去重  暂时不要删除
				// 数组去重
				// this.CustomerList = Array.from(new Set(this.CustomerList.map(JSON.stringify))).map(JSON.parse);
				// let objects = {
				// 	name: item.cCusName,
				// 	value: item.Id
				// }
				this.CustomerList.push(item)
				// 第二种去重  过滤不同id  想通名称的数组
				let newCustomerList = this.$utils.uniqueMultiPropertyArray(this.CustomerList)
				this.CustomerList = newCustomerList

			},
			// 选择客户确定后
			customerChoiceConfirm() {
				this.$emit("customerChoice", this.CustomerList);
				this.show = false;
			},
			// 获取客户列表
			getDataList() {
				console.log(this.isAllData)
				this.status = "loading"
				GetDataListForCustomerInfoRetrieval({
						PageIndex: this.pagination.current,
						PageRows: this.pagination.pageSize,
						SortField: "cCusCode",
						SortType: "desc",
						Search: this.queryParam,
						// ...this.filters
					})
					.then(resJson => {
						if (resJson.Success) {
							// 赋值总条数
							this.pagination.Total = resJson.Total;
							if (resJson.Data.length == 0 || resJson.Data == null) {
								this.status = "noMore"
								return
							}
							if (this.pagination.current == 1) {
								this.data = resJson.Data
							} else {
								// 有数据 赋值列表 加载状态结束
								this.data = this.data.concat(resJson.Data)
							}
							// 如果list长度大于等于总数据条数，数据全部加载完成
							if (this.data.length >= resJson.Total) {
								this.status = "noMore"
								return
							}

						} else {
							this.$errorToast(resJson.Msg);
							this.status = "noMore"
						}
					});
			},
			setCustomerList(list) {
				console.log('关联过来的列表', list)
				this.data = list
			},
			init() {
				this.show = true;
				this.cancel()
			},
			openForm() {
				this.init();
			}
		}
	};
</script>

<style scoped lang="less">
	// 滑动导航栏
	.log-tabbar-content {

		//设置滚动列表 **************************************
		.log-tabbar-list {
			.log-shop-item {
				white-space: nowrap; // 滚动必须加的属性
			}
		}
	}

	.search-none {
		height: 70%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.outerBox {
		/* 	height: calc(100vh - 120px);
		padding-bottom: 50px;
		box-sizing: border-box;
		overflow: auto; */
		/* #ifdef H5 */
		height: calc(100vh - 50px);
		/* #endif */
		/* #ifdef APP */
		height: calc(100vh - 220px);
		/* #endif */
	}

	.ckbxcli {
		border: 1px solid Gainsboro;
		width: 95%;
		text-align: center;
		margin-left: 2%;
		margin-top: 10px;
		box-sizing: border-box;
		padding: 20rpx;
		/* 	padding-right: 0px;
		padding-left: 0px;
		padding-bottom: 2px; */
		cursor: pointer;
		/* padding-top: 6px; */
		list-style-type: none;
		display: inline-block;
	}

	.ul {
		/* margin-top: 20px; */
	}

	.nav {
		position: fixed !important;
		width: 100%;
	}
</style>